package cn.hdu.goods.order.servlet;

import cn.hdu.goods.cart.domain.CartItem;
import cn.hdu.goods.cart.service.CartItemService;
import cn.hdu.goods.order.domain.Order;
import cn.hdu.goods.order.domain.OrderItem;
import cn.hdu.goods.order.service.OrderService;
import cn.hdu.goods.pager.PageBean;
import cn.hdu.goods.user.domain.User;
import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderService();
    private CartItemService cartItemService = new CartItemService();

    public String cancel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        int status = orderService.findStatus(oid);
        if (status != 1) {
            req.setAttribute("code", "error");
            req.setAttribute("msg", "状态不对，不能取消");
        }
        orderService.updateStatus(oid, 5);
        req.setAttribute("code", "success");
        req.setAttribute("msg", "您的订单被取消啦，你后悔去吧！");
        return "f:/jsps/msg.jsp";
    }

    public String paymentPre(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("order", orderService.load(req.getParameter("oid")));
        return "f:/jsps/order/pay.jsp";
    }

    public String confirm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        int status = orderService.findStatus(oid);
        if (status != 3) {
            req.setAttribute("code", "error");
            req.setAttribute("msg", "状态不对，不能确认收货");
        }
        orderService.updateStatus(oid, 4);
        req.setAttribute("code", "success");
        req.setAttribute("msg", "恭喜，交易成功");
        return "f:/jsps/msg.jsp";
    }

    public String load(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");

        Order order = orderService.load(oid);
        req.setAttribute("order", order);
        String btn = req.getParameter("btn");
        req.setAttribute("btn", btn);
        return "f:/jsps/order/desc.jsp";
    }


    public String myOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = getPc(req);
        String url = getUrl(req);

        User user = (User) req.getSession().getAttribute("sessionUser");
        PageBean<Order> pb = orderService.myOrders(user.getUid(), pc);

        pb.setUrl(url);
        req.setAttribute("pb", pb);
        return "f:/jsps/order/list.jsp";
    }

    public String createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String cartItemIds = req.getParameter("cartItemIds");
        List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds);


        Order order = new Order();
        order.setOid(CommonUtils.uuid());
        order.setOrdertime(String.format("%tF %<tT", new Date()));
        order.setStatus(1);
        order.setAddress(req.getParameter("address"));
        User owner = (User) req.getSession().getAttribute("sessionUser");
        order.setOwner(owner);
        BigDecimal total = new BigDecimal("0");
        for (CartItem cartItem : cartItemList) {
            total = total.add(new BigDecimal(cartItem.getSubtotal() + ""));
        }
        order.setTotal(total.doubleValue());


        List<OrderItem> orderItemList = new ArrayList<>();
        for (CartItem cartItem : cartItemList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderItemId(CommonUtils.uuid());
            orderItem.setQuantity(cartItem.getQuantity());
            orderItem.setSubtotal(cartItem.getSubtotal());
            orderItem.setBook(cartItem.getBook());
            orderItem.setOrder(order);
            orderItemList.add(orderItem);
        }
        order.setOrderItemList(orderItemList);

        orderService.createOrder(order);

        cartItemService.batchDelete(cartItemIds);

        req.setAttribute("order", order);
        return "f:/jsps/order/ordersucc.jsp";

    }

    private int getPc(HttpServletRequest req) {
        int pc = 1;
        String param = req.getParameter("pc");
        if (param != null && !param.trim().isEmpty()) {
            try {
                pc = Integer.parseInt(param);
            } catch (RuntimeException ignored) {

            }
        }
        return pc;
    }

    private String getUrl(HttpServletRequest req) {
        String url = req.getRequestURI() + "?" + req.getQueryString();
        int index = url.lastIndexOf("&pc=");
        if (index != -1) {
            url = url.substring(0, index);
        }
        return url;
    }


}
